package fr.cnam.projetSiw.client.structure.pages;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;

import fr.cnam.projetSiw.client.core.ContextKeeper;
import fr.cnam.projetSiw.client.ressources.GeneralBundle;
import fr.cnam.projetSiw.client.structure.DescEventPlace;
import fr.cnam.projetSiw.shared.MusicEvent;
import fr.cnam.projetSiw.widget.PhotoGrid;

public class HomeViewImpl extends Composite implements HomeView
{
	private Presenter presenter = null;
	
	private PhotoGrid grid = new PhotoGrid();
	
	
	private Label lblTitle = new Label("Découvrez les événements musicaux tout près de chez vous !");
	public HomeViewImpl()
	{
		initWidget(new FlowPanel());	

		HorizontalPanel pnl = new HorizontalPanel();
		
		Image img = new Image(GeneralBundle.INSTANCE.imgNoted());
		
		pnl.add(img);
		pnl.add(lblTitle);
		pnl.setCellVerticalAlignment(lblTitle, HasVerticalAlignment.ALIGN_MIDDLE);
		pnl.addStyleName(GeneralBundle.INSTANCE.css().spaceTitleHome());

		lblTitle.addStyleName(GeneralBundle.INSTANCE.css().titleSquare());
		getPanel().add(pnl);
		
		getPanel().add(grid);
		
	}
	
	@Override  
	public void setPresenter(ZicPresenter presenter)
	{
		this.presenter =(Presenter)presenter;
	}
	
	private FlowPanel getPanel()
	{
		return (FlowPanel)this.getWidget();
	}

	public void setData(ArrayList<MusicEvent> events)
	{
		int cpt = 0;
		for (final MusicEvent e : events)
		{
			if(e.getEventImg() != null)
			{
				FocusPanel pnl = grid.addCase(e);
				pnl.addClickHandler(new ClickHandler()
				{

					@Override
					public void onClick(ClickEvent event)
					{
						ContextKeeper.getInstance().setEvent(e);
						presenter.goTo(new DescEventPlace("DescPlace"));
					}
				});
				cpt++;
				if(cpt == 6)
					break;
			}
				
		}

	}

}
